package com.dou.you.che.web.controller.test;

import com.dou.you.che.cache.constants.RedisConstant;
import com.dou.you.che.cache.queue.bean.Msg;
import com.dou.you.che.cache.queue.hander.MessageTrunk;
import com.dou.you.che.cache.utils.RedisCacheUtils;
import com.dou.you.che.core.entity.sys.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 缓存测试<由于有的同学不需要缓存,故没有把这块代码集成到项目中去,有需要的同学自行处理></>
 * @Author 黄国刚【1058118328@qq.com】
 */
@RestController
@RequestMapping(value = "test/redis")
public class RedisTestAct {

    @Autowired
    private RedisCacheUtils redisCacheUtils;
    @Autowired
    private MessageTrunk messageTrunk;

    /**
     * 添加缓存
     * @param key
     * @param value
     */
    @RequestMapping(value = "add")
    public Object addSomeThing(String key,String value){

        redisCacheUtils.add(key,value);

        return "Ok";
    }

    /**
     * 查询缓存
     * @param key
     * @return
     */
    @RequestMapping(value = "get")
    public Object getSomeThing(String key){

        return redisCacheUtils.get(key);
    }

    /**
     * 模拟日志生产者
     * @param userName
     * @return
     */
    @RequestMapping(value = "create")
    public Object createLog(String userName){
        SysLog log = new SysLog();
        log.setUserName(userName);
        log.setCreateDate(new Date());
        log.setIp("121312");
        log.setOperation("test");
        log.setParams("test");
        log.setTitle("test");
        sendSysMonitorLog(log);
        return "Ok";
    }

    /**
     * 推送系统日志消息<不需要该代码的同学 可自行删除相关代码></>
     * 日志消费者-->LogHandler
     * @param log
     */
    private void sendSysMonitorLog(SysLog log){
        Msg msg = new Msg();
        msg.setContent(log);
        msg.setKey(RedisConstant.XeeSysLog);
        messageTrunk.put(msg);
    }
}
